﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UpdateServers.Command;
using UpdateServers.Models;

namespace AutoUpdaterServer
{
    /// <summary>
    /// ip服务设置
    /// </summary>
    public partial class SettingFrom : Form
    {
        public SettingFrom()
        {
            InitializeComponent();


        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_Save_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            var parm = new FileConfigModels()
            {
                IpAddr = this.textBox_Ip.Text,
                Port = (int)this.num_Port.Value,
                IsAutoStart = this.cb_isAutoStart.Checked,
            };
            //设置开机自启
            var sas = AutoStartHelper.SetAutoStart(parm.IsAutoStart,out error);
            if (!sas)
            {
                MessageBox.Show(error, "提示");
                return;
            }
            //保存配制
            FileConfigHelper.SaveFile(parm);

            this.Close();
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_Cance_Click(object sender, EventArgs e)
        {
            this.num_Port.ResetText();

        }

        private void SettingFrom_Load(object sender, EventArgs e)
        {
            var fc = FileConfigHelper.GetFileConfig();
            if (fc != null)
            {
                this.num_Port.Value = fc.Port == null ? 0 : fc.Port.Value;
                this.cb_isAutoStart.Checked = fc.IsAutoStart;
            }
            var iph = System.Net.Dns.GetHostEntry(Environment.MachineName);
            this.textBox_Ip.Text = iph.AddressList[1].ToString();

        }

        private void cb_isAutoStart_CheckedChanged(object sender, EventArgs e)
        {
            var IsAutoStart = this.cb_isAutoStart.Checked;

        }
    }
}
